package com.ruoyi.web.controller.kaoyan;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.kaoyan.domain.EducationMessage;
import com.ruoyi.kaoyan.service.EducationMessageService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.controller.kaoyan.vo.MessageVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 前台私信聊天
 * @author carpediem
 */
@Api(tags = "前台私信聊天", value = "前台私信聊天")
@RestController
@RequestMapping("/v2/message")
public class MessageApi extends BaseController {

    @Autowired
    private EducationMessageService messageService;
    @Autowired
    private ISysUserService userService;

    @PostMapping("/list")
    public AjaxResult messageList(Long receiverId){
        if (StringUtils.isNull(receiverId)){
            return AjaxResult.warn("传参错误");
        }
        Long userId = SecurityUtils.getUserId();
        LambdaQueryWrapper<EducationMessage> queryWrapper = new LambdaQueryWrapper<EducationMessage>()
                .in(StringUtils.isNotNull(userId), EducationMessage::getSender, userId, receiverId)
                .in(EducationMessage::getReceiver, userId, receiverId)
                .orderByAsc(EducationMessage::getCreateTime);
        List<EducationMessage> messageList = messageService.list(queryWrapper);
        Set<Long> collect = messageList.stream().map(EducationMessage::getSender).collect(Collectors.toSet());
        Set<Long> collect2 = messageList.stream().map(EducationMessage::getReceiver).collect(Collectors.toSet());
        List<SysUser> list = userService.list(new LambdaQueryWrapper<SysUser>().in(StringUtils.isNotEmpty(collect), SysUser::getUserId, collect));
        List<SysUser> list2 = userService.list(new LambdaQueryWrapper<SysUser>().in(StringUtils.isNotEmpty(collect2), SysUser::getUserId, collect2));
        Map<Long, String> map = list.stream().collect(Collectors.toMap(SysUser::getUserId, SysUser::getUserName));
        Map<Long, String> map2 = list2.stream().collect(Collectors.toMap(SysUser::getUserId, SysUser::getUserName));
        List<MessageVo> messageVoList = messageList.stream().map(e -> new MessageVo(e, map, map2)).collect(Collectors.toList());
        return AjaxResult.ok().list(messageVoList).build();
    }

    @PostMapping("/send")
    public AjaxResult sendMessage(String msg, Long receiverId){
        Long userId = SecurityUtils.getUserId();
        if (StringUtils.isBlank(msg)){
            return AjaxResult.warn("发送内容为空");
        }
        EducationMessage message = EducationMessage.builder()
                .sender(userId)
                .receiver(receiverId)
                .content(msg)
                .build();
        boolean save = messageService.save(message);
        return toAjax(save);
    }

}
